package com.usian.admin.listener;

import com.usian.admin.service.WemediaNewsAutoScanService;
import com.usian.model.common.constants.NewsAutoScanConstants;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
  * 功能描述：接受来自kafka的消息
  */
@Component
public class WeMediaKafkaListener {

    @Autowired
    private WemediaNewsAutoScanService wemediaNewsAutoScanServiceI;

    /**
      * 功能描述：处理自媒体文章审核的消息
      */
    @KafkaListener(topics = {NewsAutoScanConstants.WM_NEWS_AUTO_SCAN_TOPIC})
    public void listenNewsScan(ConsumerRecord<?,?> consumerRecord){
        // 优雅地处理null的，避免空指针异常
        Optional<? extends ConsumerRecord<?, ?>> recordOptional = Optional.ofNullable(consumerRecord);
        if (recordOptional.isPresent()) {
            Integer newsId = (Integer) consumerRecord.value();
            wemediaNewsAutoScanServiceI.autoScanByMediaNewsId(newsId);
        }
    }
}
